﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace WebMacros
{
    public class Scripter : IDisposable
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(string className, string windowName);
        [DllImport("user32.dll")]
        public static extern int FindWindowEx(int hWnd, int hWnd2, string className, string windowName);

        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint msg, int wParam, int lParam);

        static public string IE { get{ return "-ie"; } }
        static public string FireFox { get { return "-fx"; } }

        private Random m_random = new Random();

        static private Thread m_buttonTh= new Thread(ButtonClickMain);
        
        public int Timeout { get; set; }
        public string Script
        {
            get
            {
                return m_sb.ToString();
            }
        }

        StringBuilder m_sb = new StringBuilder();

        public iMacros.Status status;
        iMacros.AppClass m_app = new iMacros.AppClass();

        public Scripter( int tab, string command, bool newBrowser, int timeout = 30 )
        {
            Init( tab, command, newBrowser, timeout );
        }

        public void Dispose()
        {
            Exit();
        }

        public void Init( int tab, string command, bool newBrowser, int timeout )
        {
            m_sb.Clear();
            Timeout = timeout;

            status = m_app.iimInit( command, newBrowser, "", "", Timeout );

            m_sb.AppendLine( string.Format( "CODE:TAB T={0}", tab ) );
            m_sb.AppendLine( "TAB CLOSEALLOTHERS" );
        }
        
        public void Line( string text )
        {
            m_sb.AppendLine( text );
        }

        public void Click( int x, int y )
        {
            m_sb.AppendLine( string.Format( "DS CMD=CLICK X={0} Y={1} CONTENT=", x, y ) );
        }

        public void Wait( float sec )
        {
            sec += m_random.Next( 15 );
            m_sb.AppendLine( string.Format( "WAIT SECONDS={0}", sec ) );
        }

        public void GotoURL( string url )
        {
            m_sb.AppendLine( string.Format( "URL GOTO={0}", url ) );
        }

        public void Size( int x, int y )
        {
            m_sb.AppendLine( string.Format( "SIZE X={0} Y={1}", x, y ) );
        }

        public void Play( int time = 0 )
        {
            if( time == 0 )
            {
                time = Timeout;
            }

            status = m_app.iimPlay( Script, time );
            m_sb.Clear();
        }

        public string Extract( int i )
        {
            return m_app.iimGetLastExtract( i );
        }

        public void Exit()
        {
            status = m_app.iimExit( Timeout );
        }

        static public void StartButton()
        {
            m_buttonTh.Start();
        }

        static public void StopButton()
        {
            m_buttonTh.Abort();
        }

        class ButtonMessage
        {
            public enum Type : uint
            {
                WM_COMMAND = 0x0111,
                BN_CLICKED = 245,
            }

            public string ClassName { get; set; }
            public string Caption { get; set; }
            public string ChildCaption { get; set; }
            public int wParam { get; set; }
            public int lParam { get; set; }
            public Type Message { get; set; }

            public ButtonMessage( string className, string caption, string child, Type message, int w, int l)
            {
                ClassName = className;
                Caption = caption;
                ChildCaption = child;
                Message = message;
                wParam = w;
                lParam = l;
            }
            public ButtonMessage(string className, string caption, Type message, int w, int l)
            {
                ClassName = className;
                Caption = caption;
                Message = message;
                wParam = w;
                lParam = l;
            }

            public void Send()
            {
                int hWnd = FindWindow(ClassName, Caption);

                if (!string.IsNullOrEmpty(ChildCaption))
                {
                    int hWndChild = FindWindowEx(hWnd, 0, null, ChildCaption);
                    SendMessage(hWndChild, (uint)Message, wParam, lParam);
                }
                else if (hWnd > 0)
                {
                    SendMessage(hWnd, (uint)Message, wParam, lParam);
                }
            }
        }

        static private void ButtonClickMain()
        {
            ButtonMessage[] Messages = new ButtonMessage[] 
            { 
                new ButtonMessage( "WindowsForms10.Window.8.app.0.378734a",null, "Continue Trial", ButtonMessage.Type.BN_CLICKED, 0, 0),
                new ButtonMessage( "WindowsForms10.Window.8.app.0.33c0d9d",null, "Continue Trial", ButtonMessage.Type.BN_CLICKED, 0, 0),
                new ButtonMessage( null,"웹 브라우저",ButtonMessage.Type.WM_COMMAND, 0x00000006, 0x000E03EE),
                new ButtonMessage( null,"웹 페이지의 메시지",ButtonMessage.Type.WM_COMMAND, 0x00000006, 0x000E03EE),
            };

            while (true)
            {
                foreach (ButtonMessage message in Messages)
                {
                    Thread.Sleep(1);
                    message.Send();
                }
            }
        }
    }
}
